package com.atxm.mimall.product.service.impl;

import com.atxm.mimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atxm.common.utils.PageUtils;
import com.atxm.common.utils.Query;

import com.atxm.mimall.product.dao.CategoryDao;
import com.atxm.mimall.product.entity.CategoryEntity;
import com.atxm.mimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //查出所有分类
        List<CategoryEntity> entityList = baseMapper.selectList(null);
        //组装成父子的树形结构

        List<CategoryEntity> levelMenus = entityList.stream().filter(v -> v.getParentCid() == 0)
                .map(v->{
                    v.setChildren(getChildren(v,entityList));
                    return v;
                }).sorted((v1,v2)->{
                    return (v1.getSort()==null?0:v1.getSort())-(v2.getSort()==null?0:v2.getSort());
                }).collect(Collectors.toList());


        return levelMenus;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        //TODO //检查当前删除菜单是否被其它地方引用
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, paths);

        Collections.reverse(parentPath);

        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }

    private List<Long> findParentPath(Long catelogId,List<Long> paths){
        //1、收集当前节点id
        paths.add(catelogId);
        CategoryEntity byId = this.getById(catelogId);
        if(byId.getParentCid()!=0){
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;

    }

    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){

        List<CategoryEntity> children = all.stream().filter(v -> {
            return v.getParentCid() == root.getCatId();
        }).map(v -> {
            v.setChildren(getChildren(v, all));
            return v;
        }).sorted((v1, v2) -> {
            return (v1.getSort() == null ? 0 : v1.getSort()) - (v2.getSort() == null ? 0 : v2.getSort());
        }).collect(Collectors.toList());

        return children;
    }

}